
#include <iostream>
#include <atomic>
#include <thread>

std::atomic<int> a;
std::atomic<int> b;

void Thread1()
{
    int t = 1;
    a = t;
    b = 2;
}

void Thread2()
{
    while (b != 2)
        ; // 自旋等待
    if (a != 1)
    {
        std::cout << a << std::endl; // 出现错误值
    }
}

int main()
{
    while (1)
    {
        a = b = 0;

        std::thread t1(Thread1);
        std::thread t2(Thread2);

        t1.join();
        t2.join();
    }
    return 0;
}